package Demo__1;

import java.util.ArrayList;
import java.util.Scanner;

public class Test_product {    static Scanner input  = new Scanner(System.in);

    public static void main(String[] args) {

        Boolean tem = true;
        System.out.println("请输入你的账号：");
        System.out.println("请输入你的密码：");
        while(tem){
            String Name = input.next();
            String Password = input.next();
            //判断是否成功
            if(Name.equals("tutu")&&Password.equals("123")){
                tem = false;
                ShowTitle();
            }
            else {
                System.out.println("账号或者密码错误!请重新输入：");
            }
        }
    }
    public static void ShowTitle(){
        Product p1 = new Product(1,"电风扇",124.23);
        Product p2 = new Product(2,"冰箱",100.23);
        Product p3 = new Product(3,"洗衣机",1024.23);
        Product p4 = new Product(4,"电视",1124.23);
        Product p5 = new Product(5,"手电筒",10124.23);
        //ArrayList 创建对象后 调用Add方法来创建集合
        ArrayList list = new ArrayList();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);

        //打印商城的页面
        System.out.println("------欢迎tutu-------");
        System.out.println("编号\t商品\t价格");
        for(int i = 0;i<5;i++){
            Object o = list.get(i);//i是list的下标，返回的是一个object类的对象
            Product p = (Product) o;//ArrayList 创建对象时没有加范型，所以需要强制转换类型
            System.out.println(p.getId()+"\t"+p.getName()+"\t"+ChangeMOney(p.getPrice()));
        }
        System.out.println("-----------------");
        System.out.println("请输入商品号和批发数量：");
        int num = input.nextInt();
        int sum = input.nextInt();
        // 编号对应下标，所以用get 方法来返回一个对象，继续强转使用
        Object o = list.get(num-1);
        Product p = (Product) o;
        System.out.println("付款："+ChangeMOney(p.getPrice()*sum));
    }
    //这里写了个给价格加逗号的函数，返回值是String
    public static String ChangeMOney(Double price){
        StringBuffer sb = new StringBuffer(price+" ");
        for (int i = sb.indexOf(".")-3;i>0;i = i - 3){
            sb.insert(i,",");
        }
        return sb.toString();
    }
}
